package web_hr;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchWorkDay
 */
@WebServlet("/SearchWorkDay")
public class SearchWorkDay extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchWorkDay() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();
		System.out.println("start");
		//获取本月签到数据
		String username = (String)request.getParameter("username");
		System.out.println(username);
		Model time = new Model("hr_time");
		String sql = "select time from hr_time where username = '"+username+"' order by sta";
		ResultSet rs = time.eQuery(sql);
		
		//获取本月第一天周几，结果类似“星期五”
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("F");
		Calendar calendar = Calendar.getInstance();
		int mon = calendar.get(Calendar.MONTH)+1; 
		int year = calendar.get(Calendar.YEAR); 
		String str=null;
		if(mon<10){
			str = year+"-0"+mon+"-"+"01";
		}else{
			str = year+"-"+mon+"-"+"01";
		}
		System.out.println(str);
		java.util.Date firstDay = null;
		try {
			firstDay = sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String week = sdf1.format(firstDay);
		//System.out.println(week);
		int i = Integer.parseInt(week);
		int flag=0;
		List list = new LinkedList();
		for(int j=0;j<i;j++){
			list.add(" ");
		}
		try {
			while(rs.next()){
				list.add(String.valueOf(rs.getInt("time")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			time.close();
		}
		/*Iterator ite = list.iterator();
		while(ite.hasNext()){
			if(flag%7==0) pw.print("<tr>");
			pw.print("<td>"+ite.next()+"</td>");
			flag++;
			if(flag%7==0) pw.print("</tr>");
		}*/
		request.setAttribute("timeList", list);
		request.getRequestDispatcher("employee/workday.jsp").forward(request, response);
		
	}

}
